from rest_framework.pagination import PageNumberPagination, OrderedDict
from rest_framework.response import Response
from apps.common.utils.response import *

class PageNumberPagination(PageNumberPagination):
  """查第n页，每页显示n条数据"""
  page_size = 1000 # 默认每页显示多少条数据
  max_page_size = 1000 # 每页最多显示多少条数据
  page_size_query_param = 'size' # URL参数中每页显示条数的参数
  page_query_param = 'page' # URL中页码的参数
  
  
  # 进入父类 PageNumberPagination 可看响应体返回字段.
  #  def get_paginated_response(self, data):
  #  return Response(OrderedDict([
  #    ('count', self.page.paginator.count),
  #    ('next', self.get_next_link()),
  #    ('previous', self.get_previous_link()),
  #    ('results', data)
  #  ]))
 
   
  # 觉得不适用, 那就拷贝出来,重载函数, 自己多加几个字段. 
  # (可通过官方文档或直接调试得知从哪些属性获取正确的值.)
  def get_paginated_response(self, data):
    return Response(response_success(OrderedDict([
      ('page', self.page.number),
      ('size', self.page.paginator.per_page),
      ('total_count', self.page.paginator.count),
      ('total_page', self.page.paginator.num_pages),
      ('next', self.get_next_link()),
      ('previous', self.get_previous_link()),
      ('list', data)
    ])))
